package com.haoshuang.zhouzhoubang.ui.dialog

import android.content.Context
import com.blankj.utilcode.util.ScreenUtils
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.DialogShowUploadExampleBinding
import com.haoshuang.zhouzhoubang.model.bean.UploadExampleBean
import com.haoshuang.zhouzhoubang.ui.adapter.UploadExampleAdapter
import com.lxj.xpopup.core.BottomPopupView
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.vertical
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toObservableResponse

/**
 *
 * @property order_id String
 * @property type Int  类型: 1: 签到, 2: 确认上门, 3: 提交验收, 4: 不请求网络使用本地资源 5: 上传服务中图片
 * @property binding DialogShowUploadExampleBinding
 * @property data ArrayList<UploadExample>
 * @property adapter UploadExampleAdapter
 * @constructor
 */
class ShowUploadExampleDialog(context: Context, private val order_id: String, private val type: Int = 2, private val localList: MutableList<UploadExample> = mutableListOf()): BottomPopupView(context) {


    override fun getImplLayoutId(): Int {
        return R.layout.dialog_show_upload_example
    }

    private lateinit var binding: DialogShowUploadExampleBinding

    private val data = arrayListOf<UploadExample>()
    private val adapter = UploadExampleAdapter(data)

    override fun onCreate() {
        super.onCreate()
        binding = DialogShowUploadExampleBinding.bind(popupImplView)

        binding.ivClose.clickWithTrigger { dismiss() }
        binding.tvOk.clickWithTrigger { dismiss() }

        binding.recyclerView.vertical()
            .adapter = adapter

        if (type == 4){
            data.addAll(localList)
            adapter.notifyDataSetChanged()
        }else{
            getData()
        }
    }

    private fun getData(){

        RxHttp.postForm("/api/store_api/getCateDemoImg")
            .add("order_id", order_id)
            .toObservableResponse<UploadExampleBean>()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                data.clear()
                if (type == 5){
                    data.add(UploadExample("服务中示例", it.data?.demo_middle_serving_img?:""))
                }else if (type == 2){
                    data.add(UploadExample("服务前照片", it.data?.demo_before_serving_img?:""))
                    data.add(UploadExample("工具照片", it.data?.demo_tools_img?:""))
                    data.add(UploadExample("工装照片", it.data?.demo_work_clothes_img?:""))
                }else if (type == 3){
                    data.add(UploadExample("服务后照片", it.data?.demo_after_serving_img?:""))
                }else{
                    data.add(UploadExample("工装照片", it.data?.demo_work_clothes_img?:""))
                }
                adapter.notifyDataSetChanged()
            }

    }

    override fun getMaxHeight(): Int {
        return (ScreenUtils.getScreenHeight() * 0.7).toInt()
    }

}

data class UploadExample(
    val title: String,
    val url: Any
)